var uname = $.cookie('petname')
if(uname) {
	$('#login').find('span').last().text(uname)
	$('ul li:first').click(function() {
		location.href = 'user.html'
	})
	$('ul li:last').click(function() {
		$.get('/user/signout', '', function(data) {
			location.href = '/'
		})
	})

} else {
	$('#login').removeAttr('data-toggle').click(function() {
		location.href = 'denglu.html'
	})

}
$('.ask').click(function() {
	if(uname) {
		location.href = 'ans.html'
	} else {
		location.href = 'denglu.html'
	}
})
$.get({
	url: '/questions',
	data: '',
	success: function(data) {
		console.log(data)
		var mess = data.data
		console.log(mess)
		var html = ''
		for(var i = 0; i < mess.length; i++) {
			console.log(mess[i])
			var fileName = Date.parse(mess[i].time)
			console.log(fileName)
		
			html += "<section data-que='fileName' class='left'>"
			html += "<img src=/uploads/" + mess[i].petname + ".jpg>"
			html += "<h4>" + mess[i].petname + "</h4>"
			html += "<p>" + mess[i].content + "</p>"
			html += "<p>" + mess[i].time + "</p>"
			html += "</section>"
				//              $.cookie('value',fileName)

			 if ( mess[i].answers) {
                	for(var j=0; j<mess[i].answers.length; j++){
                		var ans=mess[i].answers[j]
                		console.log(ans)
                		html+="<section class='right'>"
                        html+="<h4>"+ans.petname+"</h4>"
                		html+="<p>"+ans.content+"</p>"
                		html+="<p>"+ans.time+"</p>"
                		html+="</section>"
                	}
                }
		}
		console.log(html)
		$('#aa').html(html)
		$('.left').click(function(e) {
			e.preventDefault()
			$.cookie('question', $(this).attr('data-que'))
			location.href = 'ask.html'
		})
	}
})